package com.hudilao.controller;



import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hudilao.common.BaseContext;
import com.hudilao.common.R;
import com.hudilao.dto.CustomerMembershipDTO;
import com.hudilao.dto.MembershipRegisterLoginDTO;
import com.hudilao.entity.Customer;
import com.hudilao.entity.CustomerMembership;
import com.hudilao.service.CustomerMembershipService;
import com.hudilao.service.CustomerService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;


@Slf4j
@RestController
@RequestMapping("/user/membership")
public class CustomerMembershipController {
    @Autowired
    private CustomerMembershipService customerMembershipService;

    @Autowired
    private CustomerService customerService;

    /**
     * 用户会员登录
     * @param customerMembership  接收的会员信息
     * @return R<CustomerMembership>
     */
    @PostMapping("/login")
    public R<CustomerMembership> login(HttpServletRequest request, @RequestBody CustomerMembership customerMembership){

        //将页面提交的密码进行加密处理
        String password = customerMembership.getPassword();
        password = DigestUtils.md5DigestAsHex(password.getBytes());

        //根据页面提交的会员用户名查数据库
        LambdaQueryWrapper<CustomerMembership> queryWrapper = new LambdaQueryWrapper<>();

        queryWrapper.eq(CustomerMembership::getName, customerMembership.getName());
        CustomerMembership cus = customerMembershipService.getOne(queryWrapper);

        //没有查到，返回登录失败结果
        if(cus == null){
            return R.error("用户不存在，登录失败");
        }

        //密码比对
        if(!cus.getPassword().equals(password)){
            return R.error("密码错误，登录失败");
        }

        return R.success(cus);
    }


    /**
     * 用户会员退出登录
     * @param request to get session
     * @return R<String>
     */
    @PostMapping("/logout")
    public R<String> logout(HttpServletRequest request){

        //用户未登录，不能退出
        long customerId = BaseContext.getCurrentId();
        Customer customer = customerService.getById(customerId);
        if(customer == null){
            return R.error("用户未登录，无法退出");
        }

        request.getSession().removeAttribute("customer");
        return R.success("退出成功");
    }

    /**
     * 用户注册会员
     * @param membershipRegisterLoginDTO include{code,name,phoneNumber}
     * @return R<String>
     */
    @PostMapping("/register")
    public R<String> register(@RequestBody MembershipRegisterLoginDTO membershipRegisterLoginDTO){
        //获取当前顾客
        long customerId = BaseContext.getCurrentId();

        //会员注册
        return customerMembershipService.register(membershipRegisterLoginDTO,customerId);
    }


    //管理端会员管理界面实现
    // 会员分页查询
    @GetMapping("/list")
    public R<Page<CustomerMembershipDTO>> getMembers
    (@RequestParam int page, @RequestParam int pageSize, @RequestParam(required = false) String name) {

        //构建分页查询page
        Page<CustomerMembershipDTO> pageInfo = customerMembershipService.getMembers(page, pageSize, name);

        return R.success(pageInfo);
    }

    // 管理端：会员充值
    @PostMapping("/recharge")
    public R<String> recharge(@RequestParam Long memberId, @RequestParam BigDecimal amount) {

        //会员充值
        boolean result = customerMembershipService.updateBalance(memberId, amount);
        if (result) {
            return R.success("充值成功");
        } else {
            return R.error("充值失败");
        }
    }

    // 管理端：会员减少余额
    @PostMapping("/decrease")
    public R<String> decreaseBalance(@RequestParam Long memberId, @RequestParam BigDecimal amount) {
        boolean result = customerMembershipService.decreaseBalance(memberId, amount);
        if (result) {
            return R.success("余额减少成功");
        } else {
            return R.error("余额减少失败");
        }
    }
}
